package freedoms.yousister.user;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ParseException;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import freedoms.yousister.R;
import freedoms.yousister.common.Contants;
import freedoms.yousister.common.HttpClientUtil;
import freedoms.yousister.service.RingBroadcastReceiver;
import freedoms.yousister.vo.Friend;

public class UserListActivity extends Activity {

	private Button leftButton;
	private TextView centerTextView;
	private Button rightButton;
	private UserListAdapter userListAdapter;
	private ListView userListView;
	private RingBroadcastReceiver receiver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.userlist_layout);

		leftButton = (Button) findViewById(R.id.leftButton);
		leftButton.setVisibility(View.INVISIBLE);

		centerTextView = (TextView) findViewById(R.id.centerTextView);
		centerTextView.setText("YOUSISTER");

		rightButton = (Button) findViewById(R.id.rightButton);
		rightButton.setText("INVITE");
		rightButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(UserListActivity.this, InviteActivity.class);
				startActivity(intent);
			}
		});
		receiver = new RingBroadcastReceiver();
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(RingBroadcastReceiver.RINGBROADCAST);
		registerReceiver(receiver, intentFilter);
	}

	@Override
	protected void onResume() {
		super.onResume();
		userListView = (ListView) findViewById(R.id.userlist);
		final List<Friend> userList = getData();
		userListAdapter = new UserListAdapter(UserListActivity.this, userList);
		userListView.setAdapter(userListAdapter);

		userListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Toast.makeText(UserListActivity.this, position + "", Toast.LENGTH_SHORT).show();
				String friend_user_id = userList.get(position).getFriend_user_id();
				sendYouSisterAction(friend_user_id);
			}
		});
	}

	private void sendYouSisterAction(String friend_user_id) {
		final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
		String device_id = tm.getDeviceId();
		List<NameValuePair> parameters = new ArrayList<NameValuePair>();
		parameters.add(new BasicNameValuePair("user_id", friend_user_id));
		System.out.println(device_id);
		HttpClientUtil httpClientUtil = new HttpClientUtil();
		HttpResponse response = httpClientUtil.post(Contants.YOUSISTER_URL, parameters);
		System.out.println(response.getStatusLine().getStatusCode());
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			try {
				String message = EntityUtils.toString(response.getEntity(), "utf-8");
				System.out.println(message);
			} catch (org.apache.http.ParseException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private List<Friend> getData() {
		List<Friend> list = new ArrayList<Friend>();
		final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
		String device_id = tm.getDeviceId();
		List<NameValuePair> parameters = new ArrayList<NameValuePair>();
		parameters.add(new BasicNameValuePair("device_id", device_id));
		HttpClientUtil httpClientUtil = new HttpClientUtil();
		HttpResponse response = httpClientUtil.post(Contants.GET_FRIENDS_URL, parameters);
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			try {
				String message = EntityUtils.toString(response.getEntity(), "utf-8");
				JSONObject jsonObject = null;
				try {
					jsonObject = new JSONObject(message);
					JSONArray jsonArray = jsonObject.getJSONArray("message");
					Gson gson = new Gson();
					for (int i = 0; i < jsonArray.length(); i++) {
						String json = jsonArray.opt(i).toString();
						Friend friend = gson.fromJson(json, Friend.class);
						list.add(friend);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			} catch (ParseException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			Toast.makeText(UserListActivity.this, "访问出错了，请联系管理员", Toast.LENGTH_SHORT).show();
		}
		return list;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(receiver);
	}
}
